import { _decorator, Component, Node } from 'cc';
import { UIManager } from '../../../../framework/ui/uimanager/UIManager';
import DataMgr from '../../../../common/src/util/DataMgr';
import { EVENT_NAMES } from '../../../../common/src/util/Enums';
import Config from '../../../../common/src/util/Config';
const { ccclass, property } = _decorator;

@ccclass('SlotPanel')
export class SlotPanel extends Component {
    @property({ type: [Node], displayName: "解锁按钮" })
    unlockNodes: Node[] = []

    start(): void {
        this.initListener()
        this.unlockTempSlot()
    }

    onDestroy(): void {
        gg.systemEvent.off(EVENT_NAMES.UNLOCK_TEMPORARILY_SLOT, this.unlockTempSlot, this)
    }
    
    // =======================================
    // ------------ 初始化
    // =======================================
    private initListener() {
        gg.systemEvent.on(EVENT_NAMES.UNLOCK_TEMPORARILY_SLOT, this.unlockTempSlot, this)
    }

    // =======================================
    // Component 事件
    // =======================================
    // ----------------- 通知事件 ----------------- //
    private unlockTempSlot() {
        for (var i = 0; i < this.unlockNodes.length; i++) {
            const dis = Config.maxSlotCount - DataMgr.currentMaxSlotCount
            this.unlockNodes[i].active = (dis > i)
        }
    }

    // ----------------- 点击事件 ----------------- //
    private onUnlockClicked(event, index) {
        UIManager.open("unlock:res/prefab/UnlockDialog")
    }
}


